package top.wu2020.lazy.ws.chat.application.impl;

import jakarta.annotation.Resource;
import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import top.wu2020.lazy.ws.chat.application.MailCodeRecordsApplication;
import top.wu2020.lazy.ws.chat.application.assembler.MailCodeRecordsDTOAssembler;
import top.wu2020.lazy.ws.chat.application.command.mail.MailCheckCommand;
import top.wu2020.lazy.ws.chat.application.command.mail.code.records.*;
import top.wu2020.lazy.ws.chat.application.dto.MailCodeRecordsDTO;
import top.wu2020.lazy.ws.chat.domain.model.mail.code.records.MailCodeRecords;
import top.wu2020.lazy.ws.chat.domain.model.mail.code.records.MailCodeRecordsRepository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 邮箱验证码记录
 *
 * @author Jia wei Wu
 * @date 2024/05/27 09:30 上午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl
 **/
@LazyApplication
public class MailCodeRecordsApplicationImpl implements MailCodeRecordsApplication {

    @Resource
    MailCodeRecordsRepository mailCodeRecordsRepository;

    /**
     * describe 新增邮箱验证码记录
     *
     * @param mailCodeRecordsStoryCommand 新增邮箱验证码记录
     * @return {@link Result<MailCodeRecords>} 邮箱验证码记录新增后领域对象
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<MailCodeRecords> story(MailCodeRecordsStoryCommand mailCodeRecordsStoryCommand) {
        MailCodeRecords mailCodeRecords = MailCodeRecordsDTOAssembler.INSTANCE.toMailCodeRecords(mailCodeRecordsStoryCommand);
        return mailCodeRecordsRepository.story(mailCodeRecords);
    }

    /**
     * describe 批量新增邮箱验证码记录
     *
     * @param mailCodeRecordsStoryCommandList 批量新增邮箱验证码记录
     * @return {@link Result<List<MailCodeRecords>>} 邮箱验证码记录新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<List<MailCodeRecords>> batchStory(List<MailCodeRecordsStoryCommand> mailCodeRecordsStoryCommandList) {
        List<MailCodeRecords> mailCodeRecordsList = mailCodeRecordsStoryCommandList.stream().map(MailCodeRecordsDTOAssembler.INSTANCE::toMailCodeRecords).collect(Collectors.toList());
        return mailCodeRecordsRepository.batchStory(mailCodeRecordsList);
    }

    /**
     * describe 更新邮箱验证码记录
     *
     * @param mailCodeRecordsUpdateCommand 更新邮箱验证码记录
     * @return {@link Result<MailCodeRecords>} 邮箱验证码记录领域对象
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<MailCodeRecords> updateOne(MailCodeRecordsUpdateCommand mailCodeRecordsUpdateCommand) {
        MailCodeRecords mailCodeRecords = MailCodeRecordsDTOAssembler.INSTANCE.toMailCodeRecords(mailCodeRecordsUpdateCommand);
        return mailCodeRecordsRepository.story(mailCodeRecords);
    }

    /**
     * describe 查询单个邮箱验证码记录
     *
     * @param mailCodeRecordsQueryOneCommand 查询单个邮箱验证码记录
     * @return {@link Result<MailCodeRecordsDTO>} 邮箱验证码记录DTO对象
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<MailCodeRecordsDTO> findOne(MailCodeRecordsQueryOneCommand mailCodeRecordsQueryOneCommand) {
        MailCodeRecords mailCodeRecords = MailCodeRecordsDTOAssembler.INSTANCE.toMailCodeRecords(mailCodeRecordsQueryOneCommand);
        return mailCodeRecordsRepository.findOne(mailCodeRecords).convert(MailCodeRecordsDTOAssembler.INSTANCE::fromMailCodeRecords);
    }

    /**
     * describe 查询多个邮箱验证码记录
     *
     * @param mailCodeRecordsQueryListCommand 查询多个邮箱验证码记录
     * @return {@link Result<List<MailCodeRecordsDTO>>} 邮箱验证码记录DTO对象
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<List<MailCodeRecordsDTO>> findList(MailCodeRecordsQueryListCommand mailCodeRecordsQueryListCommand) {
        MailCodeRecords mailCodeRecords = MailCodeRecordsDTOAssembler.INSTANCE.toMailCodeRecords(mailCodeRecordsQueryListCommand);
        return mailCodeRecordsRepository.findList(mailCodeRecords).convert(mailCodeRecordss -> mailCodeRecordss.stream().map(MailCodeRecordsDTOAssembler.INSTANCE::fromMailCodeRecords).collect(Collectors.toList()));
    }

    /**
     * describe 分页查询多个邮箱验证码记录
     *
     * @param mailCodeRecordsQueryListCommand 分页查询多个邮箱验证码记录
     * @return {@link Result<LazyPage<MailCodeRecordsDTO>>} 分页邮箱验证码记录DTO对象
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<LazyPage<MailCodeRecordsDTO>> findPage(int size, int current, MailCodeRecordsQueryListCommand mailCodeRecordsQueryListCommand) {
        MailCodeRecords mailCodeRecords = MailCodeRecordsDTOAssembler.INSTANCE.toMailCodeRecords(mailCodeRecordsQueryListCommand);
        return mailCodeRecordsRepository.findPage(size, current, mailCodeRecords).convert(page -> page.convert(MailCodeRecordsDTOAssembler.INSTANCE::fromMailCodeRecords));
    }

    /**
     * describe 删除邮箱验证码记录
     *
     * @param mailCodeRecordsRemoveCommand 删除邮箱验证码记录
     * @return {@link Result<MailCodeRecords>} 邮箱验证码记录
     * @author Jia wei Wu
     * @date 2024/05/27 09:30 上午
     **/

    @Override
    public Result<MailCodeRecords> remove(MailCodeRecordsRemoveCommand mailCodeRecordsRemoveCommand) {
        MailCodeRecords mailCodeRecords = MailCodeRecordsDTOAssembler.INSTANCE.toMailCodeRecords(mailCodeRecordsRemoveCommand);
        return mailCodeRecordsRepository.remove(mailCodeRecords);
    }

    /**
     * 邮箱验证码验证
     *
     * @param mailCheckCommand 验证数据
     * @return 返回结果
     */
    @Override
    public Result<?> checkVerificationCode(MailCheckCommand mailCheckCommand) {
        // 查询验证码是否存存在
        MailCodeRecords mailCodeRecords = new MailCodeRecords();
        mailCodeRecords.setVerificationCode(mailCheckCommand.getVerificationCode());
        mailCheckCommand.setEmail(mailCodeRecords.getEmail());
       return mailCodeRecordsRepository.exists(mailCodeRecords).apply(exists -> {
            if(exists){
                return ResultFactory.successOf();
            }else {
                return ResultFactory.errorOf();
            }
        });
    }
}